﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace MyLibellus
{
    public partial class GestioBook : Form
    {
        private Book b;
        DataTable dtAuthor;
        DataTable dtSubject;
        bool aceptar;
        MantenimentAuthor ma;
        MantenimentSubject ms;
        List<Author> laAuxAdd;
        List<Subject> lsAuxAdd;
        List<Author> laAuxDel;
        List<Subject> lsAuxDel;
        

        

        public GestioBook()
        {
            InitializeComponent();

            dtAuthor = DBAccess.getTable("select id_author, full_name from author");
            dtSubject = DBAccess.getTable("select id, subject from subject");
            laAuxAdd = new List<Author>();
            lsAuxAdd = new List<Subject>();
            laAuxDel = new List<Author>();
            lsAuxDel = new List<Subject>();
            
        }

        private void btnCancelarGestBook_Click(object sender, EventArgs e)
        {
            Close();
            
        }

        private void GestioBook_Load(object sender, EventArgs e)
        {
            ma = new MantenimentAuthor();
            ms = new MantenimentSubject();
            cbAuthor.DataSource = dtAuthor;
            cbAuthor.DisplayMember = "full_name";
            cbAuthor.ValueMember = "id_author";
            cbSubject.DataSource = dtSubject;
            cbSubject.DisplayMember = "subject";
            cbSubject.ValueMember = "id";

            if (b != null)
            {
                txtIsbn.Enabled = false;
                txtIsbn.Text = b.Isbn;
                txtAwards.Text = b.Awards;
                txtLongTitle.Text = b.Long_title;
                txtShortTitle.Text = b.Short_title;
                txtPublisher.Text = b.Publisher_id;
                txtSummary.Text = b.Summary;
                txtPricePaid.Text = b.Price_paid.ToString();
                plenarList(b.LA, b.LS);
                

            }
            else {
                b = new Book();
                txtIsbn.Enabled = true;
                txtIsbn.Text = "";
                txtAwards.Text = "";
                txtLongTitle.Text = "";
                txtShortTitle.Text = "";
                txtPublisher.Text = "";
                txtSummary.Text = "";
                txtPricePaid.Text = "";
                txtIsbn.Focus();
            }
            
        }

        public void plenarList(List<Author> la,List<Subject> ls )
        {
            
            foreach (Author a in la)
            {
                txtGBListAuthor.Items.Add(a.FullName);
            }
            foreach (Subject subj in ls)
            {
                txtGBListSubjects.Items.Add(subj.Nom);
            }
            
        }
       


        internal Book B
        {
            get { return b; }
            set { b = value; }
        }

        public bool Aceptar
        {
            get { return aceptar; }
            set { aceptar = value; }
        }

        private void btnAceptarGestBook_Click(object sender, EventArgs e)
        {
            
            this.Close();
            this.aceptar = true;
            b = new Book();
            b.Isbn = txtIsbn.Text;
            b.Short_title = txtShortTitle.Text;
            b.Long_title = txtLongTitle.Text;
            b.Publisher_id = txtPublisher.Text;
            b.Price_paid = txtPricePaid.Text.Length!=0?double.Parse(txtPricePaid.Text):0;
            b.Summary = txtSummary.Text;
            b.Awards = txtAwards.Text;
            
            
            
        }

        private void btnGBAfegirAuthor_Click(object sender, EventArgs e)
        {                

                    int id = int.Parse(dtAuthor.Rows[cbAuthor.SelectedIndex].ItemArray.ElementAt(0).ToString());

                    string s = dtAuthor.Rows[cbAuthor.SelectedIndex].ItemArray.ElementAt(1).ToString();


                    Author a = new Author(s, id);
                    if (!buscar(a))
                    {
                        b.LA.Add(a);

                        laAuxAdd.Add(a);
                        
                        txtGBListAuthor.Items.Add(s) ;
                    }
                
           
        }
        public bool buscar(Author a)
        {
            int i=0;
            bool trobat = false;
            while (i < b.LA.Count && !trobat)
            {
                trobat = b.LA.ElementAt(i).Id == a.Id;
                i++;
            }
            return trobat;
        }

        public bool buscar(Subject s)
        {
            int i = 0;
            bool trobat = false;
            while (i < b.LS.Count && !trobat)
            {
                trobat = b.LS.ElementAt(i).Id == s.Id;
                i++;
            }
            return trobat;
        }

        private void btnGBAfegirSubject_Click(object sender, EventArgs e)
        {
            
            int id = int.Parse(dtSubject.Rows[cbSubject.SelectedIndex].ItemArray.ElementAt(0).ToString());

            string nom = dtSubject.Rows[cbSubject.SelectedIndex].ItemArray.ElementAt(1).ToString();

            Subject subj = new Subject(id, nom);
            if (!buscar(subj))
            {
                b.LS.Add(subj);
                lsAuxAdd.Add(subj);
                   // BookRelacions.afegirBookSubject(subj, b);
                txtGBListSubjects.Items.Add(nom);
            }

        
        }

        private void txtIsbn_Leave(object sender, EventArgs e)
        {
            if (txtIsbn.Text.Length == 0)
            {
                MessageBox.Show("S'ha d'introduir l'Isbn");
                txtIsbn.Focus();
            }
        }

        private void btnGBTreureAuthor_Click(object sender, EventArgs e)
        {
            int id = txtGBListAuthor.SelectedIndex;
            if (id != -1)
            {
                Author a;
                if (id < b.LA.Count)
                    a = b.LA.ElementAt(id);
                else
                    a = laAuxAdd.ElementAt(id - b.LA.Count);

                laAuxDel.Add(a);
                txtGBListAuthor.Items.Remove(a.FullName);
            }
        }


        private void txtGBListSubjects_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void GestioBook_FormClosing(object sender, FormClosingEventArgs e)
        {
            //quan es tanca el formulari es buiden els camps.
            this.aceptar = false;
            txtGBListAuthor.Items.Clear();
            txtGBListSubjects.Items.Clear();
            this.b = null;
        }

        private void btnAuthorGestio_Click(object sender, EventArgs e)
        {
            ma.ShowDialog();
        }

        private void btnSubjectGestio_Click(object sender, EventArgs e)
        {
            ms.ShowDialog();
        }

        private void btnGBTreureSubject_Click(object sender, EventArgs e)
        {
            int id = txtGBListSubjects.SelectedIndex;
            if (id !=- 1)
            {
                Subject subj;
                if (id < b.LS.Count)
                    subj = b.LS.ElementAt(id);
                else
                    subj = lsAuxAdd.ElementAt(id - b.LS.Count); ;
                lsAuxDel.Add(subj);
                txtGBListSubjects.Items.Remove(subj.Nom);
            }
        }

        public void relacions()
        {
            Book llibre = new Book();
            llibre.Isbn = b.Isbn;
            llibre.LA = laAuxAdd;
            llibre.LS = lsAuxAdd;

            BookRelacions.afegirListBook(llibre);//Afegir la llista d'author i subjects a la BD
            llibre.LA = laAuxDel;
            llibre.LS = lsAuxDel;
            BookRelacions.elminarListBook(llibre);//s'Eliminen els authors i subjects de la BD.
 
        }


        
    }
}
